#include "printtemplate.h"
#include "ui_printtemplate.h"
#include <QMessageBox>
#include <QFile>
#include <QFileDialog>
#include "systempreset.h"
#include "constants.h"
#include "msgbox.h"
#include "log4z.h"
#include "userlogger.h"

using namespace zsummer::log4z;

PrintTemplate::PrintTemplate(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PrintTemplate)
{
    ui->setupUi(this);
    bdf = BllDataFlow::getInstance();

    //Signals and Slots
    connect(bdf, &BllDataFlow::savePdfSucceedSignal, this, &PrintTemplate::savePdfSucceedSlot);
    connect(this, &PrintTemplate::updatePrintParamSignal, bdf, &BllDataFlow::updatePrintParamSignal);
    connect(bdf, &BllDataFlow::retranslateUiSignal, this, &PrintTemplate::retranslateUiSlot);

    printTemplatePreview = new PrintTemplatePreview();
    initWidget();
}

PrintTemplate::~PrintTemplate()
{
    delete ui;
}

void PrintTemplate::generateHeaderHtml()
{
    if(companyChecked)
    {
        companyName = QString(tr("companyName:")) + QString(tr("Etta Biotech"));
    }
    else {
        companyName = "";
    }
    if(equipmentChecked)
    {
        equipName = QString(tr("equipName:")) + QString(tr("EttaEquipment"));
    }
    else {
        equipName = "";
    }
    if(serialChecked)
    {
        //todo:get equipment serial
        equipSerial= QString(tr("equipSerial:")) + "Etta123456";
    }
    else {
        equipSerial="";
    }
    headerHtml = "";
    headerHtml += "<meta charset='utf-8'>";
    headerHtml += "<p>";
    headerHtml += companyName;
    headerHtml += "&#9;";//Tab "&#9;"
    headerHtml += equipName;
    headerHtml += "&#9;";//Tab "&#9;"
    headerHtml += equipSerial;
    headerHtml += "</p><br>";
}

void PrintTemplate::generateTailHtml()
{
    if(datetimeChecked)
    {
        datetime = QString(tr("datetime:")) + currentDateTime();
    }
    else {
        datetime = "";
    }

    if(userChecked)
    {
        username = QString(tr("username:")) + currentUsername;
    }
    else {
        username = "";
    }

    tailHtml = "";  //reset
    tailHtml += "<meta charset='utf-8'>";
    tailHtml += "<p>";
    tailHtml += datetime;
    tailHtml += "&#9;"; //Tab
    tailHtml += username;
    tailHtml += "</p><br>";
}

void PrintTemplate::on_btnPreview_clicked()
{
//    setWidgetCenter(printTemplatePreview);

    //Preview header
    companyChecked = ui->cbxCompany->isChecked();
    equipmentChecked = ui->cbxEquipment->isChecked();
    serialChecked = ui->cbxSerial->isChecked();
    generateHeaderHtml();

    //Preview tail
    userChecked = ui->cbxUser->isChecked();
    datetimeChecked = ui->cbxDateTime->isChecked();
    generateTailHtml();

    ui->lblHeaderHtml->setText(headerHtml);
    ui->lblTailHtml->setText(tailHtml);
    ui->stackedWidget->setCurrentIndex(1);
}

static QString log(const QCheckBox *checkbox)
{
    return QString("%1:%2").arg(checkbox->text()).arg(checkbox->isChecked() ? "On" : "Off");
}

void PrintTemplate::on_btnSave_clicked()
{
    //save header
    companyChecked = ui->cbxCompany->isChecked();
    equipmentChecked = ui->cbxEquipment->isChecked();
    serialChecked = ui->cbxSerial->isChecked();

    //save tail
    userChecked = ui->cbxUser->isChecked();
    datetimeChecked = ui->cbxDateTime->isChecked();

    QPrinter printer;
    int resolution = printer.resolution();
    LOGFMTD("Printer resolution = %d", resolution);
    int suggestTableWidth = 600;
    //save pagesize
    if(ui->rbA4->isChecked())
    {
        pageSize = QPrinter::A4;
        suggestTableWidth = resolution*210/25.4*0.9;  //竖向打印A4纸大小=210w*297h
        gTableWidth = suggestTableWidth;
    }
    else if (ui->rbA5->isChecked()) {
        pageSize = QPrinter::A5;
        suggestTableWidth = resolution*148/25.4*0.9;  //竖向打印A5纸大小=148w*210h
        gTableWidth = suggestTableWidth;
    }

    LOGFMTD("Printer suggest table width = %d px", suggestTableWidth);
    UserLogger::log(QString("%1; %2; %3; %4; %5; %6: %7")
                    .arg(log(ui->cbxCompany))
                    .arg(log(ui->cbxEquipment))
                    .arg(log(ui->cbxSerial))
                    .arg(log(ui->cbxUser))
                    .arg(log(ui->cbxDateTime))
                    .arg(tr("Paper size"))
                    .arg(ui->rbA4->isChecked() ? "A4" : "A5"));

    saveParam();
    emit updatePrintParamSignal();

    MsgBox::information(this, tr("Print Setting"), tr("Template saved ok."));
}

void PrintTemplate::savePdfSucceedSlot(int type)
{
    if(type == ReportType::PRINT_TEPLATE_REPORT)
    {
        MsgBox::information(this, tr("Print Template"), tr("PDF saved ok."));
    }
}

void PrintTemplate::initWidget()
{
    readParam();

    ui->cbxTailCustom1->hide();
    ui->cbxTailCustom2->hide();
    ui->cbxHeadCustom1->hide();
    ui->cbxHeadCustom2->hide();

    ui->lineHeadCustom1->hide();
    ui->lineHeadCustom2->hide();
    ui->lineTailCustom1->hide();
    ui->lineTailCustom2->hide();

    ui->lblPaperSize->hide();
    ui->rbA4->hide();
    ui->rbA5->hide();
}

void PrintTemplate::readParam()
{
    companyChecked = SystemPreset::instance()->getBoolData(EnumPresetItem::CompanyChecked);
    equipmentChecked = SystemPreset::instance()->getBoolData(EnumPresetItem::EquipmentChecked);
    serialChecked = SystemPreset::instance()->getBoolData(EnumPresetItem::SerialChecked);
    userChecked = SystemPreset::instance()->getBoolData(EnumPresetItem::UserChecked);
    datetimeChecked = SystemPreset::instance()->getBoolData(EnumPresetItem::DatetimeChecked);
    pageSize = static_cast<QPrinter::PageSize>(SystemPreset::instance()->getIntData(EnumPresetItem::PageSize));

    ui->cbxCompany->setChecked(companyChecked);
    ui->cbxEquipment->setChecked(equipmentChecked);
    ui->cbxSerial->setChecked(serialChecked);
    ui->cbxUser->setChecked(userChecked);
    ui->cbxDateTime->setChecked(datetimeChecked);
    if (pageSize==0)
    {
        ui->rbA4->setChecked(true);
    }
    else if(pageSize == 9)
    {
        ui->rbA5->setChecked(true);
    }
}

void PrintTemplate::saveParam()
{
    SystemPreset::instance()->setData(EnumPresetItem::CompanyChecked, companyChecked);
    SystemPreset::instance()->setData(EnumPresetItem::EquipmentChecked, equipmentChecked);
    SystemPreset::instance()->setData(EnumPresetItem::SerialChecked, serialChecked);
    SystemPreset::instance()->setData(EnumPresetItem::UserChecked, userChecked);
    SystemPreset::instance()->setData(EnumPresetItem::DatetimeChecked, datetimeChecked);
    SystemPreset::instance()->setData(EnumPresetItem::PageSize, pageSize);
    SystemPreset::instance()->setData(EnumPresetItem::TableWidth, gTableWidth);
}

void PrintTemplate::on_btnReturn_clicked()
{
    ui->stackedWidget->setCurrentIndex(0);
}

void PrintTemplate::retranslateUiSlot()
{
    ui->retranslateUi(this);
}
